/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.GUI;

import edu.nii.qlks.ControlLayer.ThamSoControl;
import edu.nii.qlks.DataTransferObject.ThamSoInfo;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author 10520_000
 */
public class jfrm_ThayDoiSoLuongKhachToiDa extends javax.swing.JInternalFrame {

    ThamSoControl thamsoCT;
    ThamSoInfo thamsoInfo;
    /**
     * Creates new form jfrm_ThayDoiSoLuongKhachToiDa
     */
    public jfrm_ThayDoiSoLuongKhachToiDa() throws Exception {
        initComponents();
        thamsoCT = new ThamSoControl();
        thamsoInfo = new ThamSoInfo();
        Load_SoKhachToiDa();
       
    }
    public void Load_SoKhachToiDa(){
       String str = jTextField_SoLuongKhachToiDa.getText().trim();
        try {
            Vector vt = thamsoCT.LayDS("SoKhachToiDa = N'" + str + "'");
            Vector vt_row = (Vector) vt.get(0);
            jTextField_SoLuongKhachToiDa.setText(vt_row.get(0).toString().trim());
        } catch (Exception ex) {
            Logger.getLogger(jfrm_ThayDoiSoLuongKhachToiDa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jXPanel2 = new org.jdesktop.swingx.JXPanel();
        jXLabel1 = new org.jdesktop.swingx.JXLabel();
        jTextField_SoLuongKhachToiDa = new javax.swing.JTextField();
        jButton_CapNhat = new javax.swing.JButton();
        jButton_Thoat = new javax.swing.JButton();
        jXLabel2 = new org.jdesktop.swingx.JXLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        jXPanel1.setLayout(new java.awt.BorderLayout());

        jXPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jXPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin khách", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        jXPanel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXLabel1.setText("Số lượng khách tối đa");
        jXLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTextField_SoLuongKhachToiDa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField_SoKhachToiDa_Click(evt);
            }
        });
        jTextField_SoLuongKhachToiDa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_SoLuongKhachToiDaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jXPanel2Layout = new javax.swing.GroupLayout(jXPanel2);
        jXPanel2.setLayout(jXPanel2Layout);
        jXPanel2Layout.setHorizontalGroup(
            jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel2Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jTextField_SoLuongKhachToiDa, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(74, Short.MAX_VALUE))
        );
        jXPanel2Layout.setVerticalGroup(
            jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel2Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(jXPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField_SoLuongKhachToiDa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(46, Short.MAX_VALUE))
        );

        jButton_CapNhat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_CapNhat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        jButton_CapNhat.setText("Cập nhật");
        jButton_CapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CapNhatActionPerformed(evt);
            }
        });

        jButton_Thoat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jButton_Thoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
        jButton_Thoat.setText("Thoát");
        jButton_Thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_ThoatActionPerformed(evt);
            }
        });

        jXLabel2.setText("THAY ĐỔI SỐ LƯỢNG KHÁCH TỐI ĐA TRONG MỘT PHÒNG");
        jXLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton_CapNhat)
                        .addGap(18, 18, 18)
                        .addComponent(jButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(89, 89, 89))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(72, 72, 72)
                            .addComponent(jXPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 49, Short.MAX_VALUE)
                .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jXLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(jXPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton_CapNhat)
                    .addComponent(jButton_Thoat))
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_ThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_ThoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton_ThoatActionPerformed

    private void jButton_CapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CapNhatActionPerformed
      if(jTextField_SoLuongKhachToiDa.getText().equals("")){
            JOptionPane.showMessageDialog(this, "Bạn chưa nhập số lượng khách tối đa!", "Lỗi", JOptionPane.ERROR_MESSAGE);
        }
        else{
           try{
               thamsoInfo.setSoKhachToiDa(Integer.parseInt(jTextField_SoLuongKhachToiDa.getText().trim()));
               thamsoInfo.setSoKhachTheoDonGia(Integer.parseInt(jTextField_SoLuongKhachToiDa.getText().trim()) - 1);
               try {
                   thamsoCT.Update(thamsoInfo);

                    JOptionPane.showMessageDialog(this, "Cập nhật thành công!", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
                } catch (Exception ex) {
                    Logger.getLogger(jfrm_CapNhatDonGiaLoaiPhong.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(this, "Cập nhật thất bại!", "Lỗi", JOptionPane.ERROR_MESSAGE);
                }
                 
           }
           catch (Exception e){
               JOptionPane.showMessageDialog(this, "Chỉ được phép nhập số!", "Lỗi", JOptionPane.ERROR_MESSAGE);
               jTextField_SoLuongKhachToiDa.requestFocus();
               jTextField_SoLuongKhachToiDa.setText("");
               return;
           }
        }
    }//GEN-LAST:event_jButton_CapNhatActionPerformed

    private void jTextField_SoLuongKhachToiDaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField_SoLuongKhachToiDaActionPerformed
        jButton_CapNhatActionPerformed(evt);
    }//GEN-LAST:event_jTextField_SoLuongKhachToiDaActionPerformed

    private void jTextField_SoKhachToiDa_Click(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextField_SoKhachToiDa_Click
        jTextField_SoLuongKhachToiDa.setText(null);
    }//GEN-LAST:event_jTextField_SoKhachToiDa_Click

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_CapNhat;
    private javax.swing.JButton jButton_Thoat;
    private javax.swing.JTextField jTextField_SoLuongKhachToiDa;
    private org.jdesktop.swingx.JXLabel jXLabel1;
    private org.jdesktop.swingx.JXLabel jXLabel2;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXPanel jXPanel2;
    // End of variables declaration//GEN-END:variables
}
